﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.HCLoad.Parameters;
using YArchitech.HCLoad.Utils;
using YArchitech.HCLoad.XmlData;

namespace YArchitech.HCLoad.DataBinding
{
	public class DatabaseBinding
	{
		public static bool DataChanged
		{
			get
			{
				return DatabaseBinding.m_bDataChanged;
			}
			set
			{
				DatabaseBinding.m_bDataChanged = value;
			}
		}

		public static bool AppendChange(Space space, RoomLoad loadItem)
		{
			if (space == null || loadItem == null)
			{
				return false;
			}
			if (DatabaseBinding.m_dictChanges == null)
			{
				DatabaseBinding.m_dictChanges = new Dictionary<Space, List<RoomLoad>>();
			}
			List<RoomLoad> list;
			if (DatabaseBinding.m_dictChanges.ContainsKey(space))
			{
				list = DatabaseBinding.m_dictChanges[space];
			}
			else
			{
				list = new List<RoomLoad>();
				DatabaseBinding.m_dictChanges.Add(space, list);
			}
			list.Add(loadItem);
			DatabaseBinding.DataChanged = true;
			return true;
		}

		public static bool SaveConstructionChanges(string sFileName)
		{
			if (DatabaseBinding.m_dictChanges == null || DatabaseBinding.m_dictChanges.Count == 0)
			{
				return false;
			}
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
			xmlDocument.AppendChild(newChild);
			XmlElement xmlElement = xmlDocument.CreateElement("YArchitech.HCLoad.Changes");
			xmlDocument.AppendChild(xmlElement);
			foreach (KeyValuePair<Space, List<RoomLoad>> keyValuePair in DatabaseBinding.m_dictChanges)
			{
				Space key = keyValuePair.Key;
				XmlElement xmlElement2 = xmlDocument.CreateElement("Space");
				xmlElement2.SetAttribute("id", key.Id.ToString());
				xmlElement.AppendChild(xmlElement2);
				XmlElement xmlElement3 = xmlDocument.CreateElement("Name");
				xmlElement3.AppendChild(xmlDocument.CreateTextNode(key.Name));
				xmlElement2.AppendChild(xmlElement3);
				Dictionary<Space, List<RoomLoad>>.Enumerator enumerator;
                //keyValuePair = enumerator.Current;
				foreach (RoomLoad roomLoad in keyValuePair.Value)
				{
					XmlElement xmlElement4 = xmlDocument.CreateElement("Surface");
					xmlElement4.SetAttribute("id", roomLoad.GuidString);
					xmlElement4.SetAttribute("surfaceType", HcUtils.ToConstructionTypeStr(roomLoad.LoadType));
					xmlElement4.SetAttribute("constructionIdRef", roomLoad.WhTempl.ID.ToString());
					xmlElement4.SetAttribute("constructionName", roomLoad.WhTempl.Name);
					XmlElement xmlElement5 = xmlDocument.CreateElement("Name");
					XmlText newChild2 = xmlDocument.CreateTextNode(roomLoad.Name);
					xmlElement5.AppendChild(newChild2);
					xmlElement4.AppendChild(xmlElement5);
					xmlElement2.AppendChild(xmlElement4);
				}
			}
			xmlDocument.Save(sFileName);
			return true;
		}

		public static bool Binding(UIApplication rvtUIAPP, DataBase hcdb, SpaceAttributeFile spAttFile)
		{
			if (hcdb.Project == null || hcdb.Project.BuildingCollect == null)
			{
				return false;
			}
			bool flag = false;
			Transaction transaction = new Transaction(rvtUIAPP.ActiveUIDocument.Document, "YArchitech_HCLoad_CopySpaceResult");
			try
			{
				transaction.Start();
				DatabaseBinding.DataChanged = false;
				if (DatabaseBinding.m_dictChanges != null && DatabaseBinding.m_dictChanges.Count > 0)
				{
					DatabaseBinding.m_dictChanges.Clear();
				}
				IDictionaryEnumerator enumerator = hcdb.Project.BuildingCollect.GetEnumerator();
				enumerator.Reset();
				while (enumerator.MoveNext())
				{
					Building building = enumerator.Value as Building;
					if (building != null)
					{
						IDictionaryEnumerator enumerator2 = building.FloorCollect.GetEnumerator();
						enumerator2.Reset();
						while (enumerator2.MoveNext())
						{
                            YArchitech.HCLoad.XmlData.Floor floor = enumerator2.Value as YArchitech.HCLoad.XmlData.Floor;
							if (floor != null && floor.RoomCollect != null)
							{
								IDictionaryEnumerator enumerator3 = floor.RoomCollect.GetEnumerator();
								enumerator3.Reset();
								while (enumerator3.MoveNext())
								{
									Room room = enumerator3.Value as Room;
									flag |= SpaceBinding.Binding(rvtUIAPP, room, spAttFile);
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				MessageBox.Show(ex.ToString());
				return false;
			}
			finally
			{
				transaction.Commit();
			}
			if (!flag)
			{
				MessageBox.Show("更新空间数据失败，可能数据文件不是当前revit项目的负荷计算文件！");
			}
			return flag;
		}

		private static bool m_bDataChanged;

		private static Dictionary<Space, List<RoomLoad>> m_dictChanges;
	}
}
